<?php

namespace App\Http\Controllers\Index;

use App\Models\Admin\AdManage;
use App\Models\Admin\Recruitment;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use Style, DB;

class CampusrecruitController extends CommonController{
    /**
     * 校园招聘
     * @param $type
     * @return mixed
     */
    //校园招聘页面
    public function campusRecruit(){
        //获取校园招聘实习列表
        $jobList = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select('t1.id', 't1.com_id', 't1.job_title', 't1.urgent', 't1.location1', 't1.updatetime', 't1.part_status',
                't2.company_name', 't2.logo', 't1.salary_above', 't1.salary_below')
            //职位显示审核通过的
            ->where('t1.job_status', '1')
            ->where('t1.part_status', '2')
            ->orderBy('t1.updatetime', 'desc')
            ->orderBy('t1.id', 'desc')
            ->take(21)
            ->get();
        foreach($jobList as $v){
            $v->joburl = 'jobinfo_' . idEncryption($v->id) . '.html';
            $v->location1 = !empty($v->location1) ? CollArea::getAreaName($v->location1) : '';
            $v->logo = !empty($v->logo) ? '/upload/companylogo/' . $v->logo : '/default/images/index/company_default.jpg';
            $v->salary_above = !empty($v->salary_above) ? CollOption::getOptionName($v->salary_above, 'expectedsalary') : '';
            $v->salary_below = !empty($v->salary_below) ? CollOption::getOptionName($v->salary_below, 'expectedsalary') : '';
            $v->money = ($v->salary_above) ? $v->salary_above . '~' . $v->salary_below . '元/月' : '面议';
            $v->updatetime = substr($v->updatetime, 0, 10);
        }
        //登录框banner广告
        $campusX1 = AdManage::getAd('main', 'X1');
        $campusX2 = AdManage::getAd('main', 'X2');
        $campusX3 = AdManage::getAd('main', 'X3');

        //获取校园招聘实习列表
        $news = DB::table('news as t1')
            ->join('news_category as t2', 't1.category', '=', 't2.id')
            ->select('t1.id', 't1.title', 't1.category')
            //职位显示审核通过的
            ->where('t2.new_sign', 'campus')
            ->orderBy('t1.created_at', 'desc')
            ->take(16)
            ->get();
        if(!empty($news)){
            $cate_url = 'news?class=' . $news[0]->category;
        }else{
            $cate_url = 'news';
        }
        //招聘会列表
        $recruitment = Recruitment::select('id','job_title', 'hold_time', 'hold_unit', 'hold_address', 'exhibition')
            ->where('recruitment_type', 2)
            ->where('hold_time', '>=', date('Y-m-d'))
            ->orderBy('hold_time', 'asc')
            ->take(4)
            ->get();
        if(count($recruitment) < 4){
            $number = 4 - count($recruitment);
            $recruitment2 = Recruitment::select('id','job_title', 'hold_time', 'hold_unit', 'hold_address', 'exhibition')
                ->where('recruitment_type', 2)
                ->where('hold_time', '<', date('Y-m-d'))
                ->orderBy('hold_time', 'desc')
                ->take($number)
                ->get();
            $collection = collect($recruitment);
            $recruitment = $collection->merge(collect($recruitment2));
        }
        if(!empty($recruitment)){
            foreach($recruitment as $v){
                $v->week = get_week($v->hold_time);
                $v->hold_date = substr($v->hold_time,5);
            }
        }
        return Style::view("index.campusRecruit", compact('jobList', 'campusX1', 'campusX2', 'campusX3', 'news', 'cate_url', 'recruitment'));
    }

}